package com.shemy.dao;

import com.shemy.entity.User;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-04-26 17:22
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Repository
public class UserDaoImpl implements UserDao {

    private HibernateTemplate template;

    @Autowired
    public UserDaoImpl(SessionFactory sessionFactory) {
        this.template = new HibernateTemplate(sessionFactory);
    }

    public void save(User user) {
        template.saveOrUpdate(user);
    }

    public List<User> findAll() {
        DetachedCriteria dc=DetachedCriteria.forClass(User.class);
        List<User> list = (List<User>) template.findByCriteria(dc);
        return list;
    }

    public User findById(long userId) {
        return template.get(User.class,userId);
    }

    public void delete(long id) {
        User user=new User();
        user.setUserId(id);
        template.delete(user);
    }
}
